package com.ml.common.gateway.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述：redis工具类
 *
 * <p> 创建时间：Apr 29, 2018 10:07:30 PM </p> 
 *
 *@作者 小D课堂  小D
 */
@Component
public class RedisClient {

	
	
	@Autowired
	private StringRedisTemplate redisTpl; //jdbcTemplate
	@Autowired
	private RedisTemplate redisTemplate; //jdbcTemplate


	/**
	 * 判断缓存中是否有对应的value
	 *
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}

	/**
	 * 删除对应的value
	 *
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	 /**
     * 通过字符串key获取值
     * @param key 键
     * @return 值
     */
    public String get(String key){
        return key==null?null:redisTpl.opsForValue().get(key);
    }


    /**
     * 普通缓存放入
     * @param key 键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key,String value) {
         try {
			 redisTpl.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

	public Long rpush(String key, Object item) {
		return redisTemplate.opsForList().rightPush(key,item);
	}

	public List<Object> lrange(String key) {
		return redisTemplate.opsForList().range(key,0,-1);
	}

	public void trim(String key,Integer start,Integer end) {
		redisTemplate.opsForList().trim(key,start,end);
	}

	/**
	 * 功能描述：设置某个key过期时间
	 * @param key
	 * @param time
	 * @return
	 */
	  public boolean expire(String key,long time){
	        try {
	            if(time>0){
					redisTpl.expire(key, time, TimeUnit.SECONDS);
	            }
	            return true;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return false;
	        }
	    }

	  /**
	   * 功能描述：根据key 获取过期时间
	   * @param key
	   * @return
	   */
	  public long getExpire(String key){
	        return redisTpl.getExpire(key,TimeUnit.SECONDS);
	    }


	  	/**
	     * 递增
	     * @param key 键
	     * @return
	     */
	    public long incr(String key, long delta){
	        return redisTpl.opsForValue().increment(key, delta);
	    }


	    /**
	     * 递减
	     * @param key 键
	     * @param delta 要减少几
	     * @return
	     */
	    public long decr(String key, long delta){
	        return redisTpl.opsForValue().increment(key, -delta);
	    }

	//==============Map结构=====================

	//==============List结构=====================

	//==================set操作=============================
	/**
	 * 有序集合添加
	 *
	 * @param key
	 * @param value
	 * @param scoure 积分数
	 */
	public void zAdd(String key, Object value, double scoure) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		zset.add(key, value, scoure);
	}

	/**
	 * 获取两个分数之间的元素
	 *
	 * @param key
	 * @param scoure
	 * @param scoure1
	 * @return
	 */
	public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		redisTemplate.opsForValue();
		return zset.rangeByScore(key, scoure, scoure1);
	}

	/**
	 * 有序集合获取排名
	 *
	 * @param key 集合名称
	 * @param value 值
	 */
	public Long zRank(String key, Object value) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		return zset.rank(key,value);
	}


	/**
	 * 有序集合获取排名
	 *
	 * @param key
	 */
	public Set<ZSetOperations.TypedTuple<Object>> zRankWithScore(String key, long start,long end) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		Set<ZSetOperations.TypedTuple<Object>> ret = zset.rangeWithScores(key,start,end);
		return ret;
	}

	/**
	 * 有序集合添加
	 *
	 * @param key
	 * @param value
	 */
	public Double zSetScore(String key, Object value) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		return zset.score(key,value);
	}


	/**
	 * 有序集合添加分数
	 *
	 * @param key
	 * @param value
	 * @param scoure
	 */
	public void incrementScore(String key, Object value, double scoure) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		zset.incrementScore(key, value, scoure);
	}


	/**
	 * 有序集合获取排名
	 *
	 * @param key
	 */
	public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithScore(String key, long start,long end) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeByScoreWithScores(key,start,end);
		return ret;
	}

	/**
	 * 有序集合获取排名
	 *
	 * @param key
	 */
	public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithRank(String key, long start, long end) {
		ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
		Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeWithScores(key, start, end);
		return ret;
	}
	/**
	 * 有序集合获取排名
	 *
	 * @param key
	 */
	public Object hget(String key, String field) {
		Object o = redisTemplate.opsForHash().get(key, field);
		return o;
	}


}
